﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EGuayas.Web.Resources;
using EntGuayasSLA.BL;
using Ext.Net;

namespace EGuayas.Web.Licenciamiento.admin.tabs
{
    //Constantes.LFacilitador
    public partial class pgFacilitador : pgBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                CloseTabs(TabPanel1, 1);
                this.stoCateg.DataSource = new object[]
                {
                    new object[] { "AL", "Categoria I", "Desc 1"},
                    new object[] { "AK", "Categoria II", "Desc 2"}
                };
                this.stoCateg.DataBind();
            }
            catch (Exception ex)
            {
                RegistrarLog(ex);
            }
        }
        protected void CompleteEdit(object sender, DirectEventArgs e)
        {
            //this.AjaxLabel.Html = e.ExtraParams["value"];
        }
        /// <summary>
        /// Possible arguments: 
        ///     - string action
        ///     - string action, Dictionary<string, object> extraParams
        ///     - string action, Dictionary<string, object> extraParams, object serviceParams
        /// </summary>
        /// <returns></returns>
        [DirectMethod]
        public static object BindData()
        {
            return Data;
        }

        private static object[] Data
        {
            get
            {
                DateTime now = DateTime.Now;
                return new object[]
                {
                    new object[] {true, 1, 1, "Text 1",  DateTime.Now.Date,"SI" },
                    new object[] {false,1, 2, "Text 2",  DateTime.Now.Date,"NO" },
                    new object[] {false, 0, 3, "Text 3", DateTime.Now.Date ,"SI"},
                    new object[] {false, 1, 4, "Text 4", DateTime.Now.Date,"SI" },
                    new object[] {false, 1, 1, "Text 1", DateTime.Now.Date,"SI" },
                    new object[] {false, 1, 2, "Text 2", DateTime.Now.Date ,"SI"},
                    new object[] {false, 1, 3, "Text 3", DateTime.Now.Date,"SI" },
                    new object[] {false, 0, 4, "Text 4", DateTime.Now.Date,"SI" },
                    new object[] {false, 0, 1, "Text 1", DateTime.Now.Date,"NO" },
                    new object[] {false, 0, 2, "Text 2", DateTime.Now.Date,"NO" },
                    new object[] {false, 0, 3, "Text 3", DateTime.Now.Date ,"NO"},
                    new object[] {false, 0, 4, "Text 4", DateTime.Now.Date,"NO" }
              
                };
            }
        }
        protected static void GrdInbox_Command(object sender, DirectEventArgs e)
        {
            try{
            /*
                string commandName = e.ExtraParams["command"];
                long Id = Convert.ToInt64(e.ExtraParams["Id"]);
            */
            }
            catch 
            {
                //RegistrarLog(ex);
            }
        }
        [DirectMethod]
        public static string GetGrid(Dictionary<string, string> parameters)
        {
            try
            {
                // string id = parameters["id"];

                List<object> data = new List<object>();

                for (int i = 1; i <= 10; i++)
                {
                    data.Add(new { ID = "P" + i, Name = "Product " + i, Desc = "Product " + i, Fech = "" + DateTime.Now.ToLongTimeString(), Version = "Product " + i });
                }

                GridPanel grid = new GridPanel
                {
                    Height = 90,
                    EnableColumnHide = false,
                    Store = 
                { 
                    new Store 
                    { 
                        Model = {
                            new Model {
                                IDProperty = "ID",
                                Fields = 
                                {
                                    new ModelField("ID"),
                                    new ModelField("Name"),
                                    new ModelField("Desc"),
                                    new ModelField("Fech"),
                                    new ModelField("Version")
                              
                                }
                            }
                        },
                        DataSource = data
                    }
                },
                    ColumnModel =
                    {
                        Columns = { }
                    },

                };


                Column addColumn1 = new Column();
                addColumn1.Text = "Nombre";
                addColumn1.DataIndex = "Nombre";
                addColumn1.Flex = 4;
                grid.ColumnModel.Columns.Add(addColumn1);

                Column addColumn2 = new Column();
                addColumn2.Text = "Descripción";
                addColumn2.DataIndex = "Desc";
                addColumn2.Flex = 4;
                grid.ColumnModel.Columns.Add(addColumn2);

                Column addColumn3 = new Column();
                addColumn3.Text = "Fecha";
                addColumn3.DataIndex = "Fech";
                addColumn3.Flex = 4;
                grid.ColumnModel.Columns.Add(addColumn3);

                Column addColumn4 = new Column();
                addColumn4.Text = "Versión";
                addColumn4.DataIndex = "Version";
                addColumn4.Flex = 4;
                grid.ColumnModel.Columns.Add(addColumn4);

                CommandColumn commandColumn = new CommandColumn();
                commandColumn.ID = "colAction" + System.Guid.NewGuid();
                commandColumn.Flex = 1;
                //commandColumn.Text = "Visualizar";
                commandColumn.DataIndex = "Id";
                commandColumn.Commands.Add(new GridCommand { Icon = Ext.Net.Icon.Zoom, CommandName = "View", Text = "Vizualizar" });
                commandColumn.DirectEvents.Command.Event += GrdInbox_Command;
                //commandColumn.DirectEvents.Command.ExtraParams.Add(new Parameter { Name = "Id", Value = "record.data.Id" });
                //commandColumn.DirectEvents.Command.ExtraParams.Add(new Parameter { Name = "command", Value = "command" });
                grid.ColumnModel.Columns.Add(commandColumn);
                return ComponentLoader.ToConfig(grid);
            }            
            catch 
            {
                //RegistrarLog(ex);
            }
            return null;
        }

        protected void UploadClick(object sender, DirectEventArgs e)
        {
            string tpl = Mensajes.MensajeArchivoCorrecto;
            try
            {
                if (this.FileUploadField1.HasFile)
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO,
                        Title = Titulos.TituloAviso,
                        Message = string.Format(tpl, FileUploadField1.PostedFile.FileName)
                    });
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.ERROR,
                        Title = Titulos.TituloAviso,
                        Message = Mensajes.MensajeArchivoIncorrecto
                    });
                }
            }
            catch (Exception ex)
            {
                RegistrarLog(ex);
            }
        }
    }
}